import { Toast } from 'vant';

export default {
    namespaced: true,
    state: {
        orderNumberMin: 0,
        orderNumberMax: 50,

        // 所有菜品列表：包含购物车，提交订单之前
        foodList: [],
        // 已点菜品列表：提交订单之后
        orderPlacedFoodList: [],
    },
    getters: {
        orderFoodList(state) {
            const foodList = state.foodList.filter(
                (food) => food.orderNumber > state.orderNumberMin
            );
            return foodList;
        },

        allOrderNumber(state, getters) {
            const allOrderNumber = getters.orderFoodList.reduce(
                (total, food) => total + food.orderNumber,
                0
            );
            return allOrderNumber;
        },

        orderPlacedTotalNumber(state) {
            const allOrderNumber = state.orderPlacedFoodList.reduce(
                (total, food) => total + food.orderNumber,
                0
            );
            return allOrderNumber;
        },
    },
    mutations: {
        setFoodList(state, foodList) {
            state.foodList = foodList;
        },

        setFoodOrderNumber(state, { food, orderNumber }) {
            const currentFoodIndex = state.foodList.findIndex((item) => item.id === food.id);
            if (currentFoodIndex !== -1) {
                // 存在菜品
                state.foodList[currentFoodIndex].orderNumber = orderNumber;
            } else {
                // 未存在菜品
                console.error('菜品不存在');
            }
        },

        submitOrder(state) {
            state.foodList.forEach((currentFood) => {
                if (
                    currentFood.orderNumber > state.orderNumberMin &&
                    currentFood.orderNumber < state.orderNumberMax
                ) {
                    const currentFoodIndex = state.orderPlacedFoodList.findIndex(
                        (food) => food.id === currentFood.id
                    );
                    if (currentFoodIndex === -1) {
                        // 不存在，未点过改菜品
                        state.orderPlacedFoodList.push({
                            ...currentFood,
                        });
                    } else {
                        // 已存在，追加菜品orderNumber
                        // eslint-disable-next-line
                        let orderNumber = state.orderPlacedFoodList[currentFoodIndex].orderNumber + currentFood.orderNumber;
                        // eslint-disable-next-line
                        orderNumber = orderNumber > state.orderNumberMax ? state.orderNumberMax : orderNumber;
                        state.orderPlacedFoodList[currentFoodIndex].orderNumber = orderNumber;
                    }
                    // 清空购物车
                    currentFood.orderNumber = state.orderNumberMin;
                }
            });
        },
    },
    actions: {
        addFood({ state, commit }, food) {
            let orderNumber = food.orderNumber;
            new Promise((resolve, reject) => {
                if (orderNumber < state.orderNumberMax) {
                    orderNumber++;
                    commit('setFoodOrderNumber', {
                        food,
                        orderNumber,
                    });
                    resolve();
                } else {
                    Toast.fail('份数已达最大');
                    reject();
                }
            });
        },
        reduceFood({ state, commit }, food) {
            let orderNumber = food.orderNumber;
            new Promise((resolve, reject) => {
                if (orderNumber > state.orderNumberMin) {
                    orderNumber--;
                    commit('setFoodOrderNumber', {
                        food,
                        orderNumber,
                    });
                    resolve();
                } else {
                    Toast.fail('份数已达最小');
                    reject();
                }
            });
        },
    },
};
